var privacies = [ "PRIVATE", "FRIEND", "PUBLIC" ];
var privacyText = [ "Cá nhân", "Bạn bè", "Tất cả" ];
var index = 2;

jQuery(document).ready(function() {
	jQuery("#my_thought").focus(function() {
		var defaultPost = jQuery(this).attr("rel");
		if (jQuery.trim(jQuery(this).val()) == defaultPost) {
			jQuery(this).val("");
		}
		jQuery(".statusPrivacy").slideDown(100);
	});
	
	jQuery("#my_thought").blur(function() {
		var defaultPost = jQuery(this).attr("rel");
		if (jQuery.trim(jQuery(this).val()) == "") {
			jQuery(this).val(defaultPost);
			jQuery(".statusPrivacy").slideUp(100);
		}
	});
	
	init();
});

function init() {
	bindLikeAction();
	hideCommentBoxes();
	bindShowCommentBoxLink();
	
	bindShowComment();
	bindAddComment();
	bindDeleteCommentButton();
	
	bindLikeListLink();
	
	bindDeletePost();
}

function changePrivacy() {
	index++;
	if (index == privacies.length) {
		index = 0;
	}
	
	jQuery("#privacy").html(privacyText[index]);
}

function saveThought(wallOwner) {
	var thought = jQuery.trim(jQuery("#my_thought").val());
	var privacy = privacies[index];
	WallPostManager.addPost(wallOwner, thought, privacy, function(thoughtId) {
		if (thoughtId > 0) {
			// success
			index = 1;
			jQuery("#my_thought").val("");
			jQuery("#privacy").html(privacyText[index]);
			buildNewThoughtHTML(thoughtId, thought);
			init();
		} else {
			iAlert("Hệ thống đang bị lỗi, xin vui lòng thử lại");
		}
	});
}

function showMoreActivities(queryStr) {
	jQuery.ajax({
		url : ctx+"/ajax/wallActivities.html?" + queryStr,
		success : function(data) {
			jQuery("#show_more").remove();
			jQuery("#scroll_loader").before(data);
			jQuery("#scroll_loader").hide();

			init();
		},
		beforeSend : function() {
			jQuery("#scroll_loader").show();
		}
	});

}

function bindLikeAction() {
	jQuery(".like a").unbind("click").click(function() {
		$link = jQuery(this);
		rel = $link.attr("rel");
		i = rel.indexOf("__");
		component = rel.substring(0, i);
		id = rel.substring(i + 2);
		like(component, id, $link);
		return false;
	});
	jQuery(".unlike a").unbind("click").click(function() {
		$link = jQuery(this);
		rel = $link.attr("rel");
		i = rel.indexOf("__");
		component = rel.substring(0, i);
		id = rel.substring(i + 2);
		unlike(component, id, $link);
		return false;
	});
}

function like(component, componentId, $link) {
	LikeService.like(component, componentId, function(result) {
		if (result == 1) {
			$container = $link.parents(".wallActivity");
			jQuery(".like", $container).hide();
			jQuery(".unlike", $container).show();
			
			$likeList = jQuery(".likeListUser", $container);
			if ($likeList.is(":visible")) {
				html = "Bạn, " + jQuery(".like_list", $likeList).html();
				jQuery(".like_list", $likeList).html(html);
			} else {
				$likeList.show();
			}
		}
	});
}

function unlike(component, componentId, $link) {
	LikeService.unlike(component, componentId, function(result) {
		if (result == 1) {
			$container = $link.parents(".wallActivity");
			jQuery(".like", $container).show();
			jQuery(".unlike", $container).hide();
			
			$likeList = jQuery(".likeListUser", $container);
			html = jQuery(".like_list", $likeList).html();
			if (html.indexOf("Bạn, ") > -1) {
				html = html.substring(5);
				jQuery(".like_list", $likeList).html(html);
			} else {
				$likeList.hide();
			}
		}
	});
}

function buildNewThoughtHTML(id, thought) {
	template = jQuery("#thought_template").html();
	template = template.replace(/@id@/g, id);
	template = template.replace(/@thought@/g, thought);
	template = "<div class='wallActivity'>" + template + "</div>";
	jQuery(".wallContainer").prepend(jQuery(template).hide().slideDown(500));
}

function hideCommentBoxes() {
	jQuery(".commentbox").each(function() {
		$holder = jQuery(this).parents("#comment");
		if (jQuery(".comment_list > div", $holder).size() == 0) {
			jQuery(this).hide();
		}
	});
	jQuery(".commentbox .commentInput").unbind("blur").blur(function() {
		if (jQuery.trim(jQuery(this).val()) == "" || jQuery.trim(jQuery(this).val()) == "Viết trả lời...") {
			$holder = jQuery(this).parents("#comment");
			if (jQuery(".comment_list > div", $holder).size() == 0) {
				jQuery(this).parents(".commentbox").slideUp("fast");
			}
		}
	});
}

function bindShowCommentBoxLink() {
	jQuery(".show_comment_box").unbind("click").click(function() {
		$container = jQuery(this).parents(".wallActivity");
		$box = jQuery(".commentbox", $container);
		if (!$box.is(":visible")) {
			$box.slideDown("fast");
		}
		jQuery(".commentInput", $box).focus();
		return false;
	});
}

function bindLikeListLink() {
	jQuery(".like_list_link").fancybox({
		'scrolling': 'no'
	});
}

function bindDeletePost() {
	jQuery("a.postDelete").click(function() {
		id = jQuery(this).attr("rel").substring(5);
		$holder = jQuery(this).parents(".wallActivity");
		csscody.confirm('Bạn vừa thực hiện xóa một nội dung nào đó. Nếu chắc chắn với việc thực hiện này, vui lòng nhấn nút <b>Xác nhận</b> ở dưới!', 
			{
				onComplete: function(r){
					if(r == true){
						WallPostManager.deletePost(id, function(result) {
							if (result == 1) {
								$holder.slideUp("fast");
							} else {
								iAlert("Yêu cầu của bạn không thành công");
							}
						});
					}
				}
			});
	});
}